<?php
use yii\helpers\Url;
use yii\web\View;
// use yii\Web\View;
use app\assets\NewCreativeAsset;
NewCreativeAsset::register($this);
$this->title = $this->context->pageTitle.Yii::$app->params['titleSuffix'];
$this->params['breadcrumbs'] = [
  [
    'label' => \Yii::t('creative', 'Creative'),
    'url' => ['creative-list']
  ],
  \Yii::t('creative', 'Edit Creative')
];
$jsMsgs = $this->context->loadJsMsgs([
  'creativeForm'
]);
$this->registerJs("VAM.setVar('LANGUAGE_JS', '".$jsMsgs."');", View::POS_HEAD);
?>
<div class="page-header">
  <h1>
    <?=\Yii::t('creative', 'Creative')?>
    <small><?=\Yii::t('creative', 'Edit Display Creative')?></small>
  </h1>
</div>
<div class="panel panel-default">
  <div class="panel-heading">
    <i class="fa fa-plus-circle"></i>
    <?=\Yii::t('creative', 'Edit Display Creative')?>
    <ul class="list-unstyled pull-right">
      <li>
        <a href="#">
          <i class="fa fa-chevron-up"></i>
        </a>
      </li>
    </ul>
  </div>
  <div class="panel-body">
    <div class="row">
      <div class="col-sm-8 col-sm-offset-2">
        <form class="form-horizontal" id="data-frm" method="POST" action="<?php echo Url::to(['creative/new-creative-handle']);?>">
          <input class="hidden" type="text" name="id" value="<?=$creative['id']?>">
          <fieldset>
            <legend><?=\Yii::t('creative', 'Creative Info')?></legend>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="creative_name" >
                <em>*</em>
                <?=\Yii::t('creative', 'Creative Name')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="creative_name" type="text" name="name" value="<?=$creative['name']?>" placeholder="<?=\Yii::t('creative', 'Creative Name')?>">
                <div class="help-block text-error" id="creative_name_err"></div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="creative_vocation_large" >
                <em>*</em>
                <?=\Yii::t('creative', 'Vocation')?>：
              </label>
              <div class="col-sm-3">
                <select class="form-control" id="creative_vocation_large" name="parent_category">
                </select>
              </div>
              <div class="col-md-3">
                <select class="form-control" id="creative_vocation_small" name="category">
                </select>
              </div>
            </div>
            <div class="form-group hidden" >
              <label class="col-sm-3 control-label" for="dynamic_creative_flag" >
                <em>*</em>
                <?=\Yii::t('creative', 'Dynamic')?>：
              </label>
              <div class="col-sm-7">
                <label class="radio-inline">
                  <input type="radio" name="is_static" value="1" checked> <?=\Yii::t('creative', 'no')?>
                </label>
                <label class="radio-inline">
                  <input type="radio" name="is_static" value="0"> <?=\Yii::t('creative', 'yes')?>
                </label>
                <div class="help-block"></div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="impr_monitor_url">
                <?=\Yii::t('creative', 'Show Url')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="impr_monitor_url" type="text" name="show_monitor_url" value="<?=$creative['show_monitor_url']?>" placeholder="http(s)://">
                <div class="help-block text-error" id="impr_monitor_url_err"></div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="landing_page_url">
                <em>*</em>
                <?=\Yii::t('creative', 'Landing Page Url')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="landing_page_url" type="text" name="landing_page" value="<?=$creative['landing_page']?>" placeholder="http(s)://">
                <div class="help-block text-error" id="landing_page_url_err"></div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="click_monitor_url" >
                <?=\Yii::t('creative', 'Click Url')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="click_monitor_url" type="text" name="click_monitor_url" value="<?=$creative['click_monitor_url']?>" placeholder="http(s)://">
                <div class="help-block text-error" id="click_monitor_url_err"></div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="audit_target_url">
                <?=\Yii::t('creative', 'Audit Url')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="audit_target_url" type="text" name="adomain" value="<?=$creative['adomain_full']?>" placeholder="http(s)://">
                <div class="help-block text-error" id="audit_target_url_err"></div>
              </div>
            </div>
            <div class="form-group">
        <label for="belong_creative" class="col-sm-3 control-label">
                <?=\Yii::t('creative', 'Template')?>：
        </label>
              <div class="col-sm-7">
                <select class="form-control" id="belong_creative" name="template_id" placeholder="<?=\Yii::t('creative', 'Template')?>">
                <?php 
                  $templateId = $creative['template_id'];
                  foreach($templates as $template) {
                    $id = $template['id'];
                    $name = $template['name'];
                    $isSelected = '';
                    if ($id == $templateId) $isSelected = 'selected';
                    echo "<option value='$id' $isSelected>$name</option>";
                  }
                ?>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="alternative_creative_size">
               <?=\Yii::t('creative', 'Valid Size')?>：
              </label>
              <div class="col-sm-7">
                <p class="form-control-static" id="alternative_creative_size" name="alternative_creative_size">
                <?php 
                  $sizeList = [];
                  foreach ($sizes as $k => $size) {
                    $sizeList[] = $size['width'].'*'.$size['height'];
                  }
                  $sizeList = array_unique($sizeList);
                  sort($sizeList);
                  foreach ($sizeList as $k => $size) {
                    echo $size.',  ';
                    if(($k+1)%9 == 0) echo '</br>';
                  }
                ?>
                </p>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="alternative_creative_size">
                <?=\Yii::t('creative', 'Valid Material Type')?>：
              </label>
              <div class="col-sm-7">
                <p class="form-control-static" id="alternative_creative_size" name="alternative_creative_size">JPG,PNG,GIF,SWF</p>
              </div>
            </div>
            <div class="form-group" id="material_source">
              <label class="col-sm-3 control-label" for="alternative_creative_size">
                <?=\Yii::t('creative', 'Material Source')?>：
              </label>
              <div class="col-sm-7">
                <label class="radio-inline">
                  <input type="radio" name="material_source" value="0" <?if($creative['material_source'] == 0) echo "checked";?>><?=\Yii::t('creative', 'Upload Material')?>
                </label>
                <label class="radio-inline">
                  <input type="radio" name="material_source" value="1" <?if($creative['material_source'] == 1) echo "checked";?>><?=\Yii::t('creative', 'Custom Url')?>
                </label>
              </div>
            </div>
            <div id="custom_material_url">
              <div class="form-group">
                <label for="custom-url" class="control-label col-sm-3">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Material Url')?>：
                </label>
                <div class="col-sm-7">
                  <input type="text" id="custom_url" class="form-control" name="custom_url" value="<?if($creative['material_source'] == 1) echo $creative['file_url'];?>" data-err="custom_url_err">
                  <div class="help-block text-error" id="custom_url_err"></div>
                </div>
              </div>
              <div class="form-group" id="custom_adsize">
                <label class="control-label col-md-3">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Material Size')?>：
                </label>
                <label class="control-label col-md-1 pr0">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Width')?>：
                </label>
                <div class="col-md-2">
                  <input class="form-control" type="text" id="adspace_width" name="width" value="<?if($creative['material_source'] == 1) echo $creative['width'];?>" data-err="width_err">
                  <div class="help-block text-error" id="width_err"></div>
                </div>
                <label class="control-label col-md-1 pr0">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Height')?>：
                </label>
                <div class="col-md-2">
                  <input class="form-control" type="text" id="adspace_height" name="height" value="<?if($creative['material_source'] == 1) echo $creative['height'];?>" data-err="height_err">
                  <div class="help-block text-error" id="height_err"></div>
                </div>
              </div>
              <div class="form-group">
                <label for="material-type" class="col-md-3 control-label">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Material Type')?>：
                </label>
                <div class="col-md-7">
                  <select name="material_type" id="" class="form-control">
                    <option value="JPG" <?if($creative['material_source'] == 1 && strtoupper($creative['ext']) == 'JPG') echo "selected";?>>JPG</option>
                    <option value="PNG" <?if($creative['material_source'] == 1 && strtoupper($creative['ext']) == 'PNG') echo "selected";?>>PNG</option>
                    <option value="GIF" <?if($creative['material_source'] == 1 && strtoupper($creative['ext']) == 'GIF') echo "selected";?>>GIF</option>
                    <option value="SWF" <?if($creative['material_source'] == 1 && strtoupper($creative['ext']) == 'SWF') echo "selected";?>>SWF</option>
                  </select>
                </div>
              </div>
            </div>
            <div id="local_upload_material">
              <div class="form-group">
                <label class="col-sm-3 control-label" for="upload_material">
                  <em>*</em>
                  <?=\Yii::t('creative', 'Upload Material')?>：
                </label>
                <div class="col-sm-2 col-sm-offset-7">
                  <button class="btn btn-info" id="upload-material" type="button">
                    <i class="fa fa-upload"></i>
                    <?=\Yii::t('creative', 'Upload')?>
                  </button>
                </div>
                <div class="col-sm-12 mt10 text-center">
                  <div class="creative-preview-wrap">
                      <?php
                        if ($creative['material_source'] == 0) {
                          if (in_array($material['ext'], ['jpg', 'gif', 'png'])) {
                            $preview =<<<FINE
                              <img class="img-thumbnail creative_material_preview" src="$pic" style="max-width: 310px; max-height: 260px;">
FINE;
                          } else if ($material['ext'] == 'swf') {
                            $width = $material['width'];
                            $height = $material['height'];
                            $clickUrl = $creative['click_monitor_url'];
                            $preview =<<<FINE
                              <div class="img-thumbnail">
                                <div style="position: relative; z-index: 1; width: ${width}px; height: ${height}px;">
                                  <div style="position: absolute; left: 0px; top: 0px; z-index: 2; width: ${width}px; height: ${height}px;">
                                    <object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width=${width} height=${height} type="application/x-shockwave-flash">
                                      <param name="Movie" value="${pic}">
                                      <param name="FlashVars" value="#">
                                      <param name="WMode" value="Opaque">
                                      <param name="Quality" value="High">
                                      <param name="AllowScriptAccess" value="always">
                                      <param name="Scale" value="ShowAll">
                                      <param name="AllowNetworking" value="all">
                                      <param name="AllowFullScreen" value="false">
                                      <embed width="${width}px" height="${height}px" src='${pic}' quality="High" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="Opaque" allowscriptaccess="always" flashvars="#">
                                    </object>
                                  </div>
                                </div>
                              </div>
FINE;
                          } else {
                              $preview = '';
                          }
                          echo $preview;
                        }
                      ?>
                  </div>
                  <?php
                    if ($creative['material_source'] == 1) {
                      $notUpload = \Yii::t('creative', 'Not upload');
                      $out = <<<FINE
                      <img class="img-thumbnail creative_material_default" id="creative_material_default" data-src="holder.js/310x260/text:$notUpload" alt="$notUpload">
FINE;
                      echo $out;
                    }
                  ?>
                  <input id="upload_material" name="upload_material" type="hidden" value="<?if ($creative['material_source'] == 0) echo $material['id']?>">
                  <div class="help-block text-error" id="upload_material_err"></div>
                </div>
              </div>
            </div>
          </fieldset>
          <fieldset>
            <div class="form-group">
              <div class="col-sm-offset-2 col-sm-10">
                <div class="btn-form-group pull-right">
                  <input class="operate-type" type="hidden" name="operate_type" value="save">
                  <button type="submit" class="btn btn-primary" data-loading-text="<i class='fa fa-spinner fa-spin'></i> <?=\Yii::t('common', 'Saving')?>..."><?=\Yii::t('common', 'Save')?></button>
                  <a href="<?php echo Url::to(['creative/creative-list']); ?>" class="btn btn-default"><?=\Yii::t('common', 'Back')?></a>
                </div>
              </div>
            </div>
          </fieldset>
        </form>
      </div>
    </div>
  </div>
</div>
<div class="modal fade" id="upload-material-modal">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title"><?=\Yii::t('creative', 'Upload Material')?></h4>
      </div>
      <div class="modal-body">
        <div class="portlet-content">
          <span class="btn btn-success btn-sm fileinput-button">
            <i class="fa fa-plus"></i>
            <span><?=\Yii::t('creative', 'File')?></span>
            <input id="creative-id" type="hidden" name="creative_id" value="${id}" >
            <!-- The file input field used as target for the file upload widget -->
            <input type="file" multiple="" name="files[]" id="fileupload">
          </span>
          <br>
          <br>
          <!-- The global progress bar -->
          <div class="progress" id="progress">
              <div aria-valuemax="100" aria-valuemin="0" class="progress-bar progress-bar-success progress-bar-striped">0%</div>
          </div>
          <!-- The container for the uploaded files -->
          <div class="files" id="files">
            <table class="table table-striped table-bordered">
              <thead>
                <tr>
                  <th>ID</th>
                  <th><?=\Yii::t('creative', 'File Name')?></th>
                  <th><?=\Yii::t('creative', 'File Size')?>(KB)</th>
                  <th><?=\Yii::t('creative', 'Upload Time')?></th>
                  <th><?=\Yii::t('creative', 'Info')?></th>
                  <th><?=\Yii::t('creative', 'Uploader')?></th>
                </tr>
              </thead>
              <tbody></tbody>
              </table>
            </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal"><?=\Yii::t('common', 'Close')?></button>
        <button type="button" class="btn btn-primary" id="upload-confirm-operate" data-material-id=""><?=\Yii::t('common', 'Sure')?></button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<?php
  $uploadMaterialUrl = Url::to(['creative/upload-pic']);
  // $uploadMaterialUrl = Yii::getAlias('@web') . '/response/creative/uploadMaterialDataList.php';
  $creativeVocation = $categories;
  $this->registerJs("VAM.setVar('UPLOAD_MATERIAL_URL','$uploadMaterialUrl');", View::POS_HEAD);
  $this->registerJs("VAM.setVar('CREATIVE_VOCATION','$creativeVocation')", View::POS_HEAD);

  $materialPicUrl = '';
  $parentCategory = array_shift($category);
  $childCategory = array_shift($category);
  $this->registerJs("VAM.setVar('parent_category','$parentCategory')", View::POS_END);
  $this->registerJs("VAM.setVar('child_category','$childCategory')", View::POS_END);
?>
<script type="text/javascript" >
  requirejs(["<?= Yii::getAlias('@web/').Yii::$app->params['envDir'];?>/js/site.js"], function(common) {
    requirejs(["<?= Yii::getAlias('@web/').Yii::$app->params['envDir'];?>/js/creative/newCreative.js"]);
  });
</script>